﻿// =====================================================    
// Author: hexid26
// Email : hexid26@outlook.com
// Date  : 2015/07/06
// This code is for sharing
// Copyright (C)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using System.IO;
using System.Xml.Linq;

namespace ToukidenKiwamiAssist
{
    public partial class Form1 : Form
    {
        private Boolean textNameBoxHasText = false;
        private Boolean textRegisterNameBoxHasText = false;
        private System.IntPtr processAddr = IntPtr.Zero;
        private long baseAddr;          // 游戏常规数据的基地址
        private long baseAddrBattle;    // 游戏战斗数据的基地址
        private string theProcessName;  // 主进程名称
        MitamaCfgClass mitamaCfg = new MitamaCfgClass();

        private long addrName;
        private long addrRegister;
        private long addrRegisterName;

        System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();

        public Form1()
        {
            InitializeComponent();

            textNameBox.Text = "输入要修改的名字";
            textNameBox.ForeColor = Color.LightGray;
            textNameBoxHasText = false;
            textRegisterNameBox.Text = "输入要修改的名字";
            textRegisterNameBox.ForeColor = Color.LightGray;
            textRegisterNameBoxHasText = false;
            registerList.SelectedIndex = 0;

            mitamaCfg.mitamaCfgList = new BindingList<MitamaCfgClass.mitamaCfgObject>();

            listBoxMitamaCfgList.DisplayMember = "Name";
            listBoxMitamaCfgList.DataSource = mitamaCfg.mitamaCfgList;

            listBoxMitamaCfgList.MouseDoubleClick += new MouseEventHandler(listBoxMitamaCfgList_MouseDoubleClick);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer.Interval = 500;
            timer.Tick += delegate(object o, EventArgs args)
            {
                DoWork();
            };
            timer.Start();
        }

        private void DoWork()
        {
            timer.Interval = 1500;
            int res = checkGameVersion();
        }

        private byte[] toReadyBytes(string source)
        {
            source = ChineseStringUtility.ToTraditional(source);
            System.Text.Encoding enc = System.Text.Encoding.GetEncoding("shift-jis");
            return enc.GetBytes(source);
        }

        public long ReadMemoryValue(long baseAdd)
        {
            return Helper.ReadMemoryValue(baseAdd, theProcessName);
        }

        public void WriteMemory(long baseAdd, int value)
        {
            Helper.WriteMemoryValue(baseAdd, theProcessName, value);
        }

        public void WriteData(long baseAddr, byte[] data)
        {
            int cnt = 0;
            int tmp;
            while (cnt < data.Length)
            {
                if (data.Length - cnt > 1)
                {
                    tmp = (int)data[cnt + 1] * 0x100 + (int)data[cnt];
                }
                else
                {
                    tmp = data[cnt];
                }
                WriteMemory(baseAddr + cnt, tmp);
                cnt += 2;
            }
            return;
        }

        public int checkGameVersion()
        {
            string langVersion = "";
            theProcessName = "";
            string str = "";

            // 在进程列表中查找包含指定字符串的进程
            Process[] processes;
            processes = System.Diagnostics.Process.GetProcesses();
            foreach (Process p in processes)
            {
                str = p.ProcessName;
                if (str.Length < 17)
                {
                    continue;
                }
                if (str.Substring(0, 14) == "ToukidenKiwami")
                {
                    theProcessName = str;
                    langVersion = str.Substring(15, 2);
                    processAddr = p.MainModule.BaseAddress;
                    break;
                }
            }
            if (langVersion == "")
            {
                labelInfo.Text = "游戏没有运行";
                labelInfo.Update();
                langVersion = "";
                baseAddr = 0;
                baseAddrBattle = 0;
                return 0;
            }
            else if (langVersion == "en")
            {
                // 计算基础地址
                if (baseAddr == 0)
                {
                    baseAddr = ReadMemoryValue((long)processAddr + 0x78FF58);
                    baseAddrBattle = ReadMemoryValue((long)processAddr + 0x78FEF8);
                    labelInfo.Text = "已经运行英文版游戏\r\n" + baseAddr.ToString("x8").ToUpper() + "  " + baseAddrBattle.ToString("x8").ToUpper();
                    addrName = baseAddr + 0x76e8;
                    addrRegister = baseAddr + 0x7718;
                    addrRegisterName = baseAddr + 0x78d0;
                    return 1;
                }
                if (baseAddr == ReadMemoryValue((long)processAddr + 0x78FF58))
                {
                    return 2;
                }
                else
                {
                    baseAddr = 0;
                    return 0;
                }
            }
            else if (langVersion == "ja")
            {
                // 计算基础地址
                if (baseAddr == 0)
                {
                    baseAddr = ReadMemoryValue((long)processAddr + 0x78E078);
                    baseAddrBattle = ReadMemoryValue((long)processAddr + 0x78E018);
                    labelInfo.Text = "已经运行日文版游戏\r\n" + baseAddr.ToString("x8").ToUpper() + "  " + baseAddrBattle.ToString("x8").ToUpper();
                    addrName = baseAddr + 0x76e8;
                    addrRegister = baseAddr + 0x7718;
                    addrRegisterName = baseAddr + 0x78d0;
                    return 1;
                }
                if (baseAddr == ReadMemoryValue((long)processAddr + 0x78E078))
                {
                    return 2;
                }
                else
                {
                    baseAddr = 0;
                    return 0;
                }
            }
            return 0;
        }



        private void textNameBox_Enter(object sender, EventArgs e)
        {
            if (textNameBoxHasText == false)
                textNameBox.Text = "";
            textNameBox.ForeColor = Color.Black;
            textNameBox.SelectAll();
        }

        private void textNameBox_Leave(object sender, EventArgs e)
        {
            if (textNameBox.Text == "")
            {
                textNameBox.Text = "输入要修改的名字";
                textNameBox.ForeColor = Color.LightGray;
                textNameBoxHasText = false;
            }
            else
                textNameBoxHasText = true;
        }

        private void btnModifyName_Click(object sender, EventArgs e)
        {
            if (baseAddr == 0)
            {
                return;
            }
            StringBuilder text = new StringBuilder("");
            if (textNameBox.Text.Equals("输入要修改的名字") || textNameBox.Text.Equals(""))
            {
                labelNameHex.Text = "你在搞笑吧。。。";
                labelNameHex.Update();
                return;
            }
            int length = toReadyBytes(textNameBox.Text).Length;
            byte[] readyBytes = new byte[12];
            System.Buffer.BlockCopy(toReadyBytes(textNameBox.Text), 0, readyBytes, 0, length > 12 ? 12 : length);

            WriteData(addrName, readyBytes);

            foreach (byte element in readyBytes)
            {
                text.Append(((int)element).ToString("X2") + " ");
            }
            labelNameHex.Text = text.ToString();
            text.Clear();
        }

        private void textRegisterNameBox_Enter(object sender, EventArgs e)
        {
            if (textRegisterNameBoxHasText == false)
                textRegisterNameBox.Text = "";
            textRegisterNameBox.ForeColor = Color.Black;
            textRegisterNameBox.SelectAll();
        }

        private void textRegisterNameBox_Leave(object sender, EventArgs e)
        {
            if (textRegisterNameBox.Text == "")
            {
                textRegisterNameBox.Text = "输入要修改的名字";
                textRegisterNameBox.ForeColor = Color.LightGray;
                textRegisterNameBoxHasText = false;
            }
            else
                textRegisterNameBoxHasText = true;
        }

        private void btnModifyRegisterName_Click(object sender, EventArgs e)
        {
            if (baseAddr == 0)
            {
                return;
            }
            StringBuilder text = new StringBuilder("");
            if (textRegisterNameBox.Text.Equals("输入要修改的名字") || textRegisterNameBox.Text.Equals(""))
            {
                labelRegisterHex.Text = "你在搞笑吧。。。";
                labelRegisterHex.Update();
                return;
            }

            int length = toReadyBytes(textRegisterNameBox.Text).Length;
            byte[] readyBytes = new byte[16];
            System.Buffer.BlockCopy(toReadyBytes(textRegisterNameBox.Text), 0, readyBytes, 0, length > 16 ? 16 : length);

            int type = registerList.SelectedIndex;
            int id = (int)registerID.Value;
            switch (type)
            {
                case 0:     // 武器
                    WriteData(addrRegisterName + 340 + (id - 1) * 17, readyBytes);
                    break;
                case 1:     // 防具
                    WriteData(addrRegisterName + (id - 1) * 17, readyBytes);
                    break;
                case 2:     // 御魂
                    WriteData(addrRegisterName + 680 + (id - 1) * 17, readyBytes);
                    break;
            }

            foreach (byte element in readyBytes)
            {
                text.Append(((int)element).ToString("X2") + " ");
            }
            labelRegisterHex.Text = text.ToString();
            text.Clear();
        }

        private void resetRegister_Click(object sender, EventArgs e)
        {
            if (baseAddr == 0)
            {
                return;
            }
            for (int i = 0; i < 160; i++)
            {
                WriteMemory(addrRegister + i * 2, 0xffff);
            }
        }

        private void linkToCharacer_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.rikai.com/library/kanjitables/kanji_codes.sjis.shtml");
        }

        private void btnSaveToList_Click(object sender, EventArgs e)
        {
            if (baseAddr == 0)
            {
                return;
            }
            InputForm inputForm = new InputForm(-1, -1);
            inputForm.ShowDialog(this);
            if (inputForm.DialogResult == DialogResult.Cancel)
            {
                return;
            }
            string[] tmp = inputForm.rt;

            long addrEquipedIndex = baseAddr + 0x7708;
            long equipedIndex = ReadMemoryValue(addrEquipedIndex) & 0xffff;
            long addrCurWeapon = baseAddr + 0xA108 + 16 * equipedIndex;
            long addrMitima = baseAddr + 0x11578;

            MitamaCfgClass.mitamaCfgObject configObject = new MitamaCfgClass.mitamaCfgObject();
            configObject.Name = "";
            configObject.code = new int[12] { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 };
            configObject.description = "";

            configObject.code[0] = (int)(ReadMemoryValue(addrCurWeapon + 2) & 0xffff);
            configObject.code[4] = (int)(ReadMemoryValue(addrCurWeapon + 4) & 0xffff);
            configObject.code[8] = (int)(ReadMemoryValue(addrCurWeapon + 6) & 0xffff);

            for (int i = 0; i < 3; i++)
            {
                if (configObject.code[i * 4] == 65535)
                {
                    configObject.code[4 * i + 1] = 255;
                    configObject.code[4 * i + 2] = 255;
                    configObject.code[4 * i + 3] = 255;
                }
                else
                {
                    for (int j = 1; j < 4; j++)
                    {
                        configObject.code[4 * i + j] = (int)(ReadMemoryValue(addrMitima + configObject.code[4 * i] * 16 + 7 + j) & 0xff);
                    }
                }
            }

            configObject.Name = tmp[0];
            configObject.description = tmp[1];

            mitamaCfg.mitamaCfgList.Add(configObject);
            mitamaCfg.changedFlag = true;

            mitamaCfg.mitamaCfgList = new BindingList<MitamaCfgClass.mitamaCfgObject>(mitamaCfg.mitamaCfgList.OrderBy(x => x.Name).ToList());
            listBoxMitamaCfgList.DataSource = mitamaCfg.mitamaCfgList;
        }

        private void saveFile(string localFilePath)
        {
            FileStream fs = new FileStream(localFilePath, FileMode.Create);
            StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.UTF8);
            foreach (MitamaCfgClass.mitamaCfgObject element in mitamaCfg.mitamaCfgList)
            {
                sw.WriteLine(element.Name);
                for (int i = 0; i < 12; i++)
                {
                    sw.Write(element.code[i]);
                    sw.Write(";");
                }
                sw.Write(Environment.NewLine);
                sw.WriteLine("[Des]");
                sw.WriteLine(element.description);
                sw.WriteLine("[/Des]");
            }
            sw.Flush();
            sw.Close();
            fs.Close();
            return;
        }

        public void loadFile(string localFilePath)
        {
            FileStream fs = new FileStream(localFilePath, FileMode.Open);
            StreamReader sr = new StreamReader(fs, System.Text.Encoding.UTF8);
            string sLine = sr.ReadLine();
            while (sLine != null)
            {
                MitamaCfgClass.mitamaCfgObject mitamaObject = new MitamaCfgClass.mitamaCfgObject();
                mitamaObject.Name = sLine;
                sLine = sr.ReadLine();
                mitamaObject.code = new int[12];
                string[] tmp = sLine.Split(';');
                for (int i = 0; i < 12; i++)
                {
                    mitamaObject.code[i] = Convert.ToInt32(tmp[i]);
                }
                sLine = sr.ReadLine();          // [Des]
                sLine = sr.ReadLine();
                while (sLine != "[/Des]")
                {
                    if (sLine != "")
                    {
                        mitamaObject.description += sLine;
                    }
                    sLine = sr.ReadLine();
                    if (sLine != "" && sLine != "[/Des]")
                    {
                        mitamaObject.description += Environment.NewLine;
                    }
                }

                mitamaCfg.mitamaCfgList.Add(mitamaObject);

                do
                {
                    sLine = sr.ReadLine();
                } while (sLine == "");
            }


            sr.Close();
            fs.Close();
            return;
        }

        private void btnSaveFile_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Title = "保存配置文件";
            sfd.FilterIndex = 1;
            sfd.RestoreDirectory = true;
            sfd.InitialDirectory = Environment.CurrentDirectory;
            sfd.Filter = "配置文件(*.cfg)|*.cfg|All Files|*.*";

            //ofd.ShowDialog();
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                string localFilePath = sfd.FileName.ToString(); //获得文件路径 
                saveFile(localFilePath);
                mitamaCfg.changedFlag = false;
            }
            else
            {
                return;
            }
        }

        private string getSkillName(int id)
        {
            string rt = "";
            if (id < mitamaCfg.skillList.Length)
            {
                rt = mitamaCfg.skillList[id];
                return rt;
            }
            rt = "未记载";
            return rt;
        }

        private void listBoxMitamaCfgList_SelectedIndexChanged(object sender, EventArgs e)
        {
            int id = listBoxMitamaCfgList.SelectedIndex;
            if (id != -1)
            {
                int[] code = mitamaCfg.mitamaCfgList[id].code;

                textBoxCfgDescrpt.Text = mitamaCfg.mitamaCfgList[id].description;
                webBrowser.DocumentText = "";
                string tmp = "<p>";

                // 【显示详细信息】
                for (int i = 0; i < 3; i++)
                {
                    if (code[i * 4] == 65535)
                    {
                        tmp += "<p style = \"margin:5px\"><strong><span style=\"line-height: 200%; font-family: 微软雅黑; font-size: 15; color: darkred; background: paleturquoise;\">"
                            + "武器槽 " + (i + 1) + " 没有装备御魂！" +
                            "</span></strong></p>";
                    }
                    else
                    {
                        tmp += "<p style = \"margin:5px\"><strong><span style=\"line-height: 1.5; font-family: 微软雅黑; font-size: 15; color: darkred; background: paleturquoise;\">"
                            + "・" + getSkillName(code[i * 4] * 14) + "" +
                            "</span></strong>";
                        tmp += "<span style=\"font-family: 微软雅黑; font-size: 14; color: darkred; background: paleturquoise;\">"
                            + "  [" + getSkillName(code[i * 4] * 14 + 2 + 0x0b) +
                            "]<br /></span>";
                        tmp += "<span style=\"font-family: 宋体; color: blue; font-size: 14;\">"
                            + "　　" + getSkillName(code[i * 4] * 14 + 2 + code[i * 4 + 1]) + "<br />"
                            + "　　" + getSkillName(code[i * 4] * 14 + 2 + code[i * 4 + 2]) + "<br />"
                            + "　　" + getSkillName(code[i * 4] * 14 + 2 + code[i * 4 + 3]) + "</span></p>";
                    }
                }
                tmp += "</p>";
                webBrowser.DocumentText = tmp;
                webBrowser.Update();
                //webBrowser.Document.ExecCommand("SelectAll",true,null);
                //webBrowser.Document.ExecCommand("Copy", true, null);
            }
            else
            {
                textBoxCfgDescrpt.Text = "";
            }
        }

        private void btnLoadFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "选择配置文件";
            ofd.FilterIndex = 1;
            ofd.RestoreDirectory = true;
            ofd.InitialDirectory = Environment.CurrentDirectory;
            ofd.Filter = "配置文件(*.cfg)|*.cfg|All Files|*.*";

            //ofd.ShowDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                string localFilePath = ofd.FileName.ToString(); //获得文件路径
                mitamaCfg.mitamaCfgList.Clear();
                loadFile(localFilePath);
                mitamaCfg.mitamaCfgList = new BindingList<MitamaCfgClass.mitamaCfgObject>(mitamaCfg.mitamaCfgList.OrderBy(x => x.Name).ToList());
                listBoxMitamaCfgList.DataSource = mitamaCfg.mitamaCfgList;
                mitamaCfg.changedFlag = false;
                webBrowser.Document.ExecCommand("Refresh", false, null);
            }
            else
            {
                return;
            }

        }

        private void editCurrentObject()
        {
            int id = listBoxMitamaCfgList.SelectedIndex;
            if (id == -1)
            {
                return;
            }
            else
            {
                string[] tmp;
                InputForm inputForm = new InputForm(mitamaCfg.mitamaCfgList[id].Name, mitamaCfg.mitamaCfgList[id].description, -1, -1);
                inputForm.ShowDialog();
                if (inputForm.DialogResult == DialogResult.Cancel)
                {
                    return;
                }
                tmp = inputForm.rt;
                MitamaCfgClass.mitamaCfgObject obj = mitamaCfg.mitamaCfgList[id];
                //mitamaCfg.mitamaCfgList[id];
                obj.Name = tmp[0];
                obj.description = tmp[1];
                mitamaCfg.mitamaCfgList[id] = obj;
                mitamaCfg.changedFlag = true;

                mitamaCfg.mitamaCfgList = new BindingList<MitamaCfgClass.mitamaCfgObject>(mitamaCfg.mitamaCfgList.OrderBy(x => x.Name).ToList());
                listBoxMitamaCfgList.DataSource = mitamaCfg.mitamaCfgList;

                for (int cnt = 0; cnt < mitamaCfg.mitamaCfgList.Count; cnt++)
                {
                    if (mitamaCfg.mitamaCfgList[cnt].Name.Equals(tmp[0])&&mitamaCfg.mitamaCfgList[cnt].description.Equals(tmp[1]))
                    {
                        listBoxMitamaCfgList.SelectedIndex = cnt;
                        break;
                    }
                }
                return;
            }
        }

        private void listBoxMitamaCfgList_MouseDoubleClick(object sender, EventArgs e)
        {
            editCurrentObject();
        }

        private void btnShare_Click(object sender, EventArgs e)
        {

            if (listBoxMitamaCfgList.SelectedIndex == -1)
            {
                return;
            }

            MitamaCfgClass.mitamaCfgObject obj = mitamaCfg.mitamaCfgList[listBoxMitamaCfgList.SelectedIndex];
            string tmp = "";
            tmp += obj.Name + Environment.NewLine;
            foreach (int element in obj.code)
            {
                tmp += element.ToString() + ";";
            }
            tmp += Environment.NewLine + "[Des]" + Environment.NewLine + obj.description + Environment.NewLine + "[/Des]";
            ShareForm form = new ShareForm(tmp);
            form.ShowDialog(this);
        }

        private void btnDelChosed_Click(object sender, EventArgs e)
        {
            int id = listBoxMitamaCfgList.SelectedIndex;
            if (id == -1)
            {
                return;
            }
            else
            {
                mitamaCfg.mitamaCfgList.RemoveAt(id);
                mitamaCfg.changedFlag = true;
                return;
            }
        }

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            if (mitamaCfg.changedFlag == true)
            {
                DialogResult result = MessageBox.Show("配魂列表可能有变动，需要保存么？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (result == DialogResult.OK)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Title = "保存配置文件";
                    sfd.FilterIndex = 1;
                    sfd.RestoreDirectory = true;
                    sfd.InitialDirectory = Environment.CurrentDirectory;
                    sfd.Filter = "配置文件(*.cfg)|*.cfg|All Files|*.*";

                    //ofd.ShowDialog();
                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        string localFilePath = sfd.FileName.ToString(); //获得文件路径 
                        saveFile(localFilePath);
                        e.Cancel = false;
                    }
                    else
                    {
                        e.Cancel = false;
                    }
                }
                else
                {
                    e.Cancel = false;
                }
            }
            e.Cancel = false;
        }

        private void btnApplyToGame_Click(object sender, EventArgs e)
        {
            if (baseAddr == 0)
            {
                return;
            }
            int id = listBoxMitamaCfgList.SelectedIndex;
            if (id == -1)
            {
                return;
            }
            long addrEquipedIndex = baseAddr + 0x7708;
            long equipedIndex = ReadMemoryValue(addrEquipedIndex) & 0xffff;
            long addrCurWeapon = baseAddr + 0xA108 + 16 * equipedIndex;
            long addrMitima = baseAddr + 0x11578;
            //mitamaCfg.mitamaCfgList[id]
            int[] code = mitamaCfg.mitamaCfgList[id].code;
            string noGet = "";
            for (int i = 0; i < 3; i++)
            {
                if (code[i * 4] == 65535)
                {
                    WriteMemory(addrCurWeapon + 2 * (i + 1), code[i * 4]);
                    continue;
                }

                //判定是否已经获得了御魂，没有跳过
                if ((ReadMemoryValue(addrMitima + code[i * 4] * 16 + 5) & 0xff) == 0)
                {
                    noGet += "[" + mitamaCfg.mitamaList[code[i * 4]] + "] ";
                    continue;
                }
                WriteMemory(addrCurWeapon + 2 * (i + 1), code[i * 4]);
                WriteMemory(addrMitima + code[i * 4] * 16 + 4, 0x0b0b);
                WriteMemory(addrMitima + code[i * 4] * 16 + 8, code[i * 4 + 1] + code[i * 4 + 2] * 0x100);
                WriteMemory(addrMitima + code[i * 4] * 16 + 10, code[i * 4 + 3] + 0x0b00);
                WriteMemory(addrMitima + code[i * 4] * 16 + 12, 0x0b);
            }
            if (noGet.Equals(""))
            {
                MessageBox.Show(mitamaCfg.mitamaCfgList[id].Name + "：已经装备");
            }
            else
            {
                MessageBox.Show(noGet + "\r\n\r\n以上御魂还没有入手，请继续努力！");
            }
        }

        private void btnAddConfig_Click(object sender, EventArgs e)
        {
            ShareForm form = new ShareForm();
            form.ShowDialog(this);
            if (form.DialogResult != DialogResult.OK)
            {
                return;
            }
            string text = form.rt;
            StringReader sr = new StringReader(text);

            string sLine = sr.ReadLine();
            try
            {
                while (sLine != "" && sr.Peek() != -1)
                {
                    MitamaCfgClass.mitamaCfgObject mitamaObject = new MitamaCfgClass.mitamaCfgObject();
                    mitamaObject.Name = sLine;
                    sLine = sr.ReadLine();
                    mitamaObject.code = new int[12];
                    string[] tmp = sLine.Split(';');
                    for (int i = 0; i < 12; i++)
                    {
                        mitamaObject.code[i] = Convert.ToInt32(tmp[i]);
                    }
                    sLine = sr.ReadLine();          // [Des]
                    sLine = sr.ReadLine();
                    while (sLine != "[/Des]" && sLine != null)
                    {
                        if (sLine != "")
                        {
                            mitamaObject.description += sLine + Environment.NewLine;
                            sLine = sr.ReadLine();
                        }
                        else
                        {
                            if (sr.Peek() == -1)
                            {
                                MessageBox.Show("输入有误");
                                return;
                            }
                            else
                            {
                                sLine = sr.ReadLine();
                            }
                        }
                    }

                    mitamaCfg.mitamaCfgList.Add(mitamaObject);

                    do
                    {
                        sLine = sr.ReadLine();
                    } while (sLine == "");

                    mitamaCfg.changedFlag = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            mitamaCfg.mitamaCfgList = new BindingList<MitamaCfgClass.mitamaCfgObject>(mitamaCfg.mitamaCfgList.OrderBy(x => x.Name).ToList());
            listBoxMitamaCfgList.DataSource = mitamaCfg.mitamaCfgList;
        }
    }
}
